package cn.cvs.controller;

import cn.cvs.dao.RoomMapper;
import cn.cvs.pojo.Room;
import cn.cvs.pojo.UHistory;
import cn.cvs.pojo.User;
import cn.cvs.service.RoomService;
import cn.cvs.service.UHistoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class RoomController {
    @Resource
    private RoomService roomService;
    @Resource
    private UHistoryService uHistoryService;
    @GetMapping("/index")
    public String toZhuJieMian(HttpSession session){
        List<Room> danRoom = roomService.getRoomList("单人房","","");
        List<Room> shuangRoom = roomService.getRoomList("双人房", "", "");
        List<Room> haiRoom = roomService.getRoomList("海景房", "", "");
        List<Room> shangRoom = roomService.getRoomList("商务房", "", "");
        List<Room> haoRoom = roomService.getRoomList("豪华房", "", "");
        session.setAttribute("danRoom",danRoom);
        session.setAttribute("shuangRoom",shuangRoom);
        session.setAttribute("haiRoom",haiRoom);
        session.setAttribute("shangRoom",shangRoom);
        session.setAttribute("haoRoom",haoRoom);
        return "zhuJieMian";
    }
    @GetMapping("/checkSchedule")
    public void toSchedule(@RequestParam("rId")Integer rId, HttpSession session, HttpServletResponse response,@RequestParam("isSchedule")String isSchedule,@RequestParam("isMove")String isMove) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Room room = roomService.getRoomByRId(rId);
        if("已入住".equals(isMove)){
            out.print("<script>alert('房间已有上帝入住,请重新选择!');location.href='index';</script>");
        }
        if("已预订".equals(isSchedule)){
            out.print("<script>alert('房间已被预订,请重新选择!');location.href='index';</script>");
        }
            session.setAttribute("room",room);
            out.print("<script>location.href='toSchedule';</script>");
    }
    @GetMapping("/toSchedule")
    public String toSchedule(HttpSession session){
        Object roomO = session.getAttribute("room");
        session.setAttribute("roomO",roomO);
        return "schedule";
    }
    @GetMapping("/xiaoFei")
    public String toXiaoFei(HttpSession session){

        User user = (User)session.getAttribute("user");
        List<UHistory> uHistoryList = uHistoryService.getUHistoryList("",user.getuPhone());
        session.setAttribute("uHistoryList",uHistoryList);
        return "xiaoFei";
    }
    @PostMapping("/addRoom")
    public String add(@RequestParam("roomNo") String roomNo,@RequestParam("rName") String rName,@RequestParam("rDespertion") String rDespertion,@RequestParam("price") String price){
        Room room = new Room();
        room.setRoomNo(roomNo);
        room.setPrice((Double.parseDouble(price)));
        room.setrName(rName);
        room.setrDespertion(rDespertion);
        int count = roomService.addRoom(room);
        if(count==1){
            return "zhuJieMian";
        }
        return "add";
    }
    @ResponseBody
    @PostMapping("/roomcheck")
    public  Object check(@RequestParam("roomNo")String roomNo){
        Room room = roomService.getListInfo(roomNo);
        if(room!=null){
            return  true;
        }
        return  false;
    }
    @PostMapping("/selectUHistory")
    public Object selectUHistory(@RequestParam("firstDate")String firstDate,HttpSession session){
        User user = (User)session.getAttribute("user");
        List<UHistory> uHistoryList = uHistoryService.getUHistoryList(firstDate,user.getuPhone());
        session.setAttribute("uHistoryList",uHistoryList);
        return "xiaoFei";
    }
    @ResponseBody
    @PostMapping("/deleteUHistory")
    public Object toDeleteUHistory(@RequestParam("userId")Integer userId){
        int count = uHistoryService.deleteByUserId(userId);
        boolean flag=false;
        if(count==1){
            flag=true;
        }
        return flag;
    }
}
